package com.aaa.check.service.impl;

import com.aaa.check.dao.CheckResultDao;
import com.aaa.check.dao.DocheckDao;
import com.aaa.check.dao.OrderChargeItem;
import com.aaa.check.service.CheckResultService;
import com.aaa.check.vo.CheckListVo;
import com.aaa.check.vo.CheckResultVo;
import com.aaa.check.vo.UrlVo;
import com.aaa.entity.CheckResult;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Consumer;

@Service
public class CheckResultServiceImpl implements CheckResultService {
    @Autowired
    private CheckResultDao checkResultDao;
    @Autowired
    private DocheckDao docheckDao;
    @Autowired
    private OrderChargeItem orderChargeItem;

    @Override
    public Result<IPage<CheckResult>> findByConditionn(Integer currentPage, Integer pageSize, CheckResultVo checkResultVo) {

        IPage<CheckResult> page = new Page<>(currentPage,pageSize);
        QueryWrapper<CheckResult> wrappers = new QueryWrapper<>();
        String [] checkItem =checkResultVo.getCheckItem();

        if (StringUtils.hasText(checkResultVo.getRegId())){
            wrappers.like("reg_id",checkResultVo.getRegId());
        }
        if (StringUtils.hasText(checkResultVo.getPatientName())){
            wrappers.like("patient_name",checkResultVo.getPatientName());
        }
        if (StringUtils.hasText(checkResultVo.getStatus())){
            wrappers.eq("result_status",checkResultVo.getStatus());
        }
        if (checkItem.length==5){
            wrappers.and(new Consumer<QueryWrapper<CheckResult>>() {
                             @Override
                             public void accept(QueryWrapper<CheckResult> wrapper) {
                                 wrapper.eq("check_item_id", checkItem[0]).or().eq("check_item_id", checkItem[1]).or().
                                         eq("check_item_id", checkItem[2]).or().eq("check_item_id", checkItem[3]).or().
                                         eq("check_item_id", checkItem[4]);
                             }
                         }
            );
        }
        else if (checkItem.length==4){
            wrappers.and(new Consumer<QueryWrapper<CheckResult>>() {
                             @Override
                             public void accept(QueryWrapper<CheckResult> wrapper) {
                                 wrapper.eq("check_item_id", checkItem[0]).or().eq("check_item_id", checkItem[1]).or().
                                         eq("check_item_id", checkItem[2]).or().eq("check_item_id", checkItem[3]);
                             }
                         }
            );
        }else if (checkItem.length==3){
            wrappers.and(new Consumer<QueryWrapper<CheckResult>>() {
                             @Override
                             public void accept(QueryWrapper<CheckResult> wrapper) {
                                 wrapper.eq("check_item_id", checkItem[0]).or().eq("check_item_id", checkItem[1]).or().
                                         eq("check_item_id", checkItem[2]);
                             }
                         }
            );
        }else if (checkItem.length==2){
            wrappers.and(new Consumer<QueryWrapper<CheckResult>>() {
                             @Override
                             public void accept(QueryWrapper<CheckResult> wrapper) {
                                 wrapper.eq("check_item_id", checkItem[0]).or().eq("check_item_id", checkItem[1]);
                             }
                         }
            );
        }else if (checkItem.length==1){
            wrappers.eq("check_item_id",checkItem[0]);
        }


        IPage<CheckResult> page1 = checkResultDao.selectPage(page, wrappers);
        return new Result<IPage<CheckResult>>(200,"查询用户成功",page1);
    }

//    检查结果录入
    @Transactional
    @Override
    public Result<Boolean> uptResult(String cocId, String textarea, UrlVo urlVo) {
        String itemId = cocId;
        Boolean a = docheckDao.updateStatus(itemId);
        Boolean c = orderChargeItem.updateStatus(itemId);

        String url = urlVo.getUrl();

        Long  k =checkResultDao.updRe(cocId , textarea , url);
        boolean b = k > 0 ? true : false;

        return new Result<Boolean>(200,"添加成功",b);
    }
}

